﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//-------------------------------------------------------------------------------------

using System;
using System.IO;
using System.Windows.Forms;

namespace DotNet.WinForm
{
    using DotNet.Service;
    using DotNet.Utilities;
    using DotNet.WinForm.Message;
    using DotNet.WinForm.Utilities;

    /// <summary>
    /// Program
    ///
    /// 修改纪录
    ///
    ///		2007.10.25 版本：1.0 DotNet 创建。
    ///
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2007.10.25</date>
    /// </author>
    /// </summary>
    internal static class Program
    {
        public static FrmMessage frmMessage = null;

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            // 从配置文件读取数据表的设定
            // TableConfigHelper.Instance.GetConfig(Application.StartupPath + "\\SqlMap\\Common\\BasePermissionTable.xml", new BasePermissionTable());
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.Skins.SkinManager.EnableFormSkins();

            //读取语言配置
            BaseSystemInfo.CurrentLanguage = ConfigHelper.GetValue("CurrentLanguage");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(BaseSystemInfo.CurrentLanguage);

            // 读取配置文件
            BaseSystemInfo.Version = ConfigHelper.GetValue("Version");

            // 是否检查在线状态
            BaseSystemInfo.CheckOnLine = false;
            BaseSystemInfo.AllowNullPassword = true;

            // 是否记录日志
            BaseSystemInfo.RecordLog = true;

            // 是否加密密码
            // BaseSystemInfo.EncryptPassword = true;
            // 是否需要注册
            BaseSystemInfo.NeedRegister = false;

            BaseSystemInfo.IPAddress = BaseSystemInfo.GetIPAddress();
            BaseSystemInfo.MACAddress = BaseSystemInfo.GetMacAddress();
            BaseSystemInfo.StartupPath = Application.StartupPath;

            // BaseUserConfig.Instance.LogOnTo = "Access";
            BaseSystemInfo.AppIco = Path.Combine(Application.StartupPath, "Resource/App.ico");
            BaseSystemInfo.FormIco = Path.Combine(Application.StartupPath, "Resource/Form.ico");
            BaseSystemInfo.FormBackGround = Path.Combine(Application.StartupPath, "Resource/logo3.png");

            BaseSystemInfo.ClientCache = true;

            // 是否按编号获得所有子节点
            // BasePermissionScopeManager.GetChildrensByCode = true;

            // 主应用程序集名
            BaseSystemInfo.MainAssembly = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;

            // 多语言国际化加载
            ResourceManagerWrapper.Instance.LoadResources(Path.Combine(Application.StartupPath, "Resource/Localization/"));
            // 从当前指定的语言包读取信息
            AppMessage.GetLanguageResource();

            // 读取配置文件
            BaseSystemInfo.ConfigurationFrom = ConfigurationCategory.UserConfig;
            ConfigHelper.GetConfig();

            // 初始化服务
            ServiceManager.Instance.InitService();

            // 分级管理，按什么来计算，是否按编号计算子节点
            //  BasePermissionScopeManager.GetChildrensByCode = true;

            // 设置数据库中的表配置

            // 用户是否需要配置模块菜单权限
            BaseSystemInfo.UseModulePermission = true;

            // 用户权限范围的开关
            BaseSystemInfo.UsePermissionScope = true;

            // 用户授权范围的开关
            BaseSystemInfo.UseLicensedPermission = true;

            // 用户表字段权限的开关
            BaseSystemInfo.UseTableColumnsPermission = true;
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(BaseSystemInfo.AppSkinName);

            // 按配置的登录页面进行登录，这里需要运行的是主程序才可以
            Form mainForm = BaseInterfaceLogic.GetForm(BaseSystemInfo.MainAssembly, BaseSystemInfo.MainForm);
            Application.Run(mainForm);
        }

        #region public static void InitService(BaseUserInfo userInfo) 加载服务

        /// <summary>
        /// 多线程加载服务
        /// </summary>
        public static void InitService(BaseUserInfo userInfo)
        {
            // 加载内部部门
            ClientCache.Instance.DTOrganize = null;
            ServiceManager.Instance.InitService();
            ClientCache.Instance.GetOrganizeDT(userInfo);
        }

        #endregion public static void InitService(BaseUserInfo userInfo) 加载服务
    }
}